package com.xdf.docmanagementsystem.controller;

import com.xdf.docmanagementsystem.common.util.JWTUtil;
import com.xdf.docmanagementsystem.common.util.ObjectUtil;
import com.xdf.docmanagementsystem.config.JWTToken;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import com.xdf.docmanagementsystem.vo.SysUserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xdf.docmanagementsystem.entity.ResponseResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.xdf.docmanagementsystem.entity.SysUser;
import com.xdf.docmanagementsystem.service.SysUserService;

/**
 * @Description : 自动生成的控制层
 * @Author : ZK
 * @Date : 2020-06-19 18:48:28
 */
@Api(tags = "用户")
@CrossOrigin
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    private static Mapper mapper = new DozerBeanMapper();
    @Autowired
    private SysUserService service;


    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(SysUser record) throws Exception {
        service.insertSelective(record);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 批量新增
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "批量新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/multiInsert", method = RequestMethod.POST)
    public ResponseResult<Void> multiInsert(@RequestBody List<SysUser> list) throws Exception {
        service.multiInsert(list);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 选择修改
     * @Return : SysUser
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(SysUser record) throws Exception {
        service.updateByPrimaryKeySelective(record);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 批量修改
     * @Return : SysUser
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "批量修改", httpMethod = "POST", notes = "自动生成的修改方法")
    @RequestMapping(value = "/multiUpdate", method = RequestMethod.POST)
    public ResponseResult<Void> multiUpdate(@RequestBody List<SysUser> list) throws Exception {
        service.multiUpdate(list);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 根据主键查询
     * @Return : SysUser
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public SysUser selectByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<SysUser> query(SysUser record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequestMapping(value = "/deleteByPk/{id}", method = RequestMethod.POST)
    public ResponseResult<Void> deleteByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        service.deleteByPrimaryKey(id);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 查询方法
     * [sysUser]
     * @Return : java.util.List<com.xdf.docmanagementsystem.vo.SysUserVo>
     * @Author : zhangke
     * @Date : 2020/6/20 17:55
     */
    @RequestMapping(value = "/listQueryByParam", method = {RequestMethod.GET, RequestMethod.POST})
    public List<SysUserVo> listQueryByParam(SysUser sysUser) throws Exception {
        List<SysUser> list = service.listQuery(sysUser);
        return ObjectUtil.conventList(mapper, list, SysUserVo.class);
    }

    /**
     * @param account
     * @return
     * @throws Exception
     */
    @PostMapping("/account")
    public ResponseResult<String> selectByAccount(String account) throws Exception {
        SysUser sysUser = service.selectByUserAccount(account);
        if (sysUser == null)
            return new ResponseResult<String>(false, ResponseEnum.NO_USERNAME.getState(), ResponseEnum.NO_USERNAME.getMessage());
        if (sysUser.getUserStatus() != 1) return new ResponseResult<String>(false, "该用户已停用!");
        return new ResponseResult<String>(true, ResponseEnum.OK.getState(), sysUser.getUserName());
    }

    @PostMapping("/getName")
    public ResponseResult<String> getUserNameByJWT(String token) throws Exception {
        if (StringUtils.isEmpty(token))
            return new ResponseResult<String>(false, ResponseEnum.NO_USERNAME.getState(), ResponseEnum.NO_USERNAME.getMessage(), null);
        String account = JWTUtil.getUserNameFromToken(token);
        SysUser sysUser = service.selectByUserAccount(account);
        if (sysUser == null)
            return new ResponseResult<String>(false, ResponseEnum.NO_USERNAME.getState(), ResponseEnum.NO_USERNAME.getMessage(), null);
        return new ResponseResult<String>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage(), sysUser.getUserName());
    }

    /**
     * 登录方法
     *
     * @param userAccount
     * @param userPassword
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public ResponseResult<String> login(String userAccount, String userPassword) throws Exception {
        Assert.state(!StringUtils.isEmpty(userAccount), ResponseEnum.NOT_ALLOW.getMessage());
        Subject subject = SecurityUtils.getSubject();
        SysUser user = service.selectByUserAccount(userAccount);
        if (user == null)
            return new ResponseResult<String>(false, "该用户不存在!");
        if (!Objects.equals(user.getUserPassword(), userPassword))
            return new ResponseResult<String>(false, "密码不正确!!");
        String token = JWTUtil.createJWT(user.getUserAccount(), user.getUserPassword());
        JWTToken jwtToken = new JWTToken(token);
        subject.login(jwtToken);
        return new ResponseResult<String>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage(), token);
    }

}